<?php

namespace JsonLowCode\Factory;

use BadMethodCallException;
use Closure;
use JsonLowCode\Support\Fluent;
use JsonLowCode\Traits\Macroable;
use JsonLowCode\Contracts\FactoryInstance;

/**
 * TableColumnFactory 工厂类
 */
abstract class ColumnFactory extends Fluent implements FactoryInstance
{

    use Macroable;

    /**
     * display函数集合
     * @var array<string,Closure>
     */
    protected array $displays = [];

    /**
     * 扩展类方法集合
     * @var array
     */
    protected array $methods = [

    ];

    public function __construct(){
        parent::__construct();
//        $columns = config('lowcode.table_column',[]);
//        $methods = array_merge($this->methods,$columns);
//        foreach ($methods as $method=>$class){
//            self::macro($method,$class);
//        }
    }

    /**
     * Dynamically handle calls to the class.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return mixed
     *
     * @throws BadMethodCallException
     */
    public function __call($method, $parameters)
    {
        $macro = "\\JsonLowCode\\Table\\Column\\" . ucfirst($method);
        if (!class_exists($macro)) {
            if (!static::hasMacro($method)) {
                throw new BadMethodCallException(sprintf(
                    'Method %s::%s does not exist.', static::class, $method
                ));
            }
            $macro = static::$macros[$method];
        }
        $parameters[] = $this;
        $object = new $macro(...$parameters);
        if ($object instanceof Closure) {
            return call_user_func_array($object->bindTo($this, static::class),$parameters);
        }
        return $object;
    }
}
